//
//  ViewController.m
//  XMScreenLockDemo
//
//  Created by sfk-ios on 2018/1/5.
//  Copyright © 2018年 sfk-JasonSu. All rights reserved.
//

#import "ViewController.h"
#import "XMScreenLockController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton *setPwdBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 80, 180, 60)];
    setPwdBtn.backgroundColor = [UIColor orangeColor];
    [setPwdBtn setTitle:@"设置锁屏密码" forState:UIControlStateNormal];
    [setPwdBtn addTarget:self action:@selector(setPwd) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:setPwdBtn];
    
    UILabel *tipsL = [[UILabel alloc]initWithFrame:CGRectMake(20, 140, 180, 20)];
    tipsL.font = [UIFont systemFontOfSize:12];
    tipsL.text = @"  提示：默认密码1234";
    [self.view addSubview:tipsL];
    
    UIButton *showLockBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 200, 180, 60)];
    showLockBtn.backgroundColor = [UIColor redColor];
    [showLockBtn setTitle:@"弹出锁屏" forState:UIControlStateNormal];
    [showLockBtn addTarget:self action:@selector(showLock) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:showLockBtn];
}

/// 设置锁屏密码
- (void)setPwd
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请设置锁屏密码" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput];
    UITextField *textF = [alert textFieldAtIndex:0];
    textF.placeholder = @"输入4位密码";
    textF.keyboardType = UIKeyboardTypeNumberPad;
    [alert show];
}

/// 显示锁屏界面
- (void)showLock
{
    XMScreenLockController *lockVc = [[XMScreenLockController alloc]init];
    lockVc.view.backgroundColor = [UIColor blackColor];
    lockVc.modalPresentationStyle = UIModalPresentationOverFullScreen | UIModalTransitionStyleCrossDissolve;
    [self presentViewController:lockVc animated:YES completion:nil];
}


#pragma mark - alertView delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        UITextField *textF = [alertView textFieldAtIndex:0];
        NSString *pwd = textF.text;
        if (pwd.length == 4) {
            
            // 保存密码到本地
            [[NSUserDefaults standardUserDefaults] setObject:pwd forKey:PWD_KEY];
            [[NSUserDefaults standardUserDefaults]synchronize];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"设置成功 ^_^" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请输入4位密码！" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
}
@end
